package com.hushui.mall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import java.util.HashMap;

/**
 * @Author: hwh
 * @Date: 2023/03/18/15:24
 */

@Configuration
public class MyRabbitMQConfig {

    /* 容器中的Queue、Exchange、Binding 会自动创建（在RabbitMQ）不存在的情况下 */

    /**
     * 死信队列
     *
     * @return
     */@Bean
    public Queue orderDelayQueue() {
        /*
            Queue(String name,  队列名字
            boolean durable,  是否持久化
            boolean exclusive,  是否排他
            boolean autoDelete, 是否自动删除
            Map<String, Object> arguments) 属性
         */
        HashMap<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 60000); // 消息过期时间 1分钟
        Queue queue = new Queue("order.delay.queue", true, false, false, arguments);

        return queue;
    }

    /**
     * 普通队列
     *
     * @return
     */
    @Bean
    public Queue orderReleaseQueue() {

        return new Queue("order.release.order.queue", true, false, false);
    }

    /**
     * TopicExchange
     *
     * @return
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("order-event-exchange");
    }


    @Bean
    public Binding orderCreateBinding( TopicExchange orderEventExchange,Queue orderDelayQueue) {
        /*
         * String destination, 目的地（队列名或者交换机名字）
         * DestinationType destinationType, 目的地类型（Queue、Exhcange）
         * String exchange,
         * String routingKey,
         * Map<String, Object> arguments
         * */
//        return new Binding("order.delay.queue",
//                Binding.DestinationType.QUEUE,
//                "order-event-exchange",
//                "order.create.order",
//                null);
        return BindingBuilder.bind(orderDelayQueue).to(orderEventExchange).with("#.order.create.order.#");
    }

    @Bean
    public Binding orderReleaseBinding() {

        return BindingBuilder.bind(orderReleaseQueue()).to(topicExchange()).with("#.order.release.order.#");
    }

//    /**
//     * 订单释放直接和库存释放进行绑定
//     * @return
//     */
//    @Bean
//    public Binding orderReleaseOtherBinding() {
//
////        return new Binding("stock.release.stock.queue",
////                Binding.DestinationType.QUEUE,
////                "order-event-exchange",
////                "order.release.other.#",
////                null);
//        return BindingBuilder.bind("")
//    }


    /**
     * 商品秒杀队列
     * @return
     */
    @Bean
    public Queue orderSecKillOrderQueue() {
        return  new Queue("order.seckill.order.queue");
    }

    /**
     * 秒杀队列绑定
     * @param orderSecKillOrderQueue
     * @param orderEventExchange
     * @return
     */
    @Bean
    public Binding orderSecKillOrderQueueBinding(Queue orderSecKillOrderQueue, TopicExchange orderEventExchange) {

        return BindingBuilder.bind(orderSecKillOrderQueue).to(orderEventExchange).with("#.order.seckill.order.#");
    }


}
